package com.hc.adapter;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hc.DTO.AnimalDTO;
import com.hc.DTO.MessageDTO;
import com.hc.adapter.AnimalAdapter.viewHolder;
import com.hc.appanimal.R;
import com.hc.constants.Constants;
import com.hc.ultil.PicassoUtils;

public class MessagesAdapter extends ArrayAdapter<Object> {
	Context mContext;
	private ArrayList<Object> arrObject;

	public MessagesAdapter(Context context, int resource, ArrayList<Object> arr) {
		super(context, resource, arr);
		mContext = context;
		arrObject = arr;
	}

	class viewHolder {
		TextView txtDescExp;
		TextView txtUpdatedOn;
		TextView txtcount;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrObject.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return arrObject.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = null;
		if (convertView == null) {
			rowView = inflater
					.inflate(R.layout.row_list_message, parent, false);
		} else {
			rowView = convertView;
		}

		viewHolder holder = new viewHolder();

		// holder.icon = (ImageView) rowView.findViewById(R.id.icon);
		// holder.txtImageName = (TextView)
		// rowView.findViewById(R.id.txtImageName);

		holder.txtDescExp = (TextView) rowView.findViewById(R.id.txtDescExp);
		holder.txtUpdatedOn = (TextView) rowView
				.findViewById(R.id.txtUpdatedOn);
		holder.txtcount = (TextView) rowView.findViewById(R.id.txtcount);
		MessageDTO dto = (MessageDTO) arrObject.get(position);
		try {
			if (!dto.getDescExp().equalsIgnoreCase("")) {
				holder.txtDescExp.setText(dto.getDescExp());
			} else {
				holder.txtDescExp.setText("");
			}
			String date = "";
			String datefinal = "";
			if (!dto.getUpdatedOn().equalsIgnoreCase("")) {
				date = dto.getUpdatedOn();
				String year = date.substring(0, 4);
				String mon = date.substring(5, 7);
				String day = date.substring(8, 10);
				int hour = Integer.parseInt(date.substring(11, 13));
				String minutes = date.substring(14, 16);

				datefinal = day + "-" + mon + "-" + year;
				if (hour <= 12) {
					datefinal = "Sent on: " + day + "-" + mon + "-" + year
							+ " " + hour + ":" + minutes + " AM";
				} else {
					datefinal = "Sent on: " + day + "-" + mon + "-" + year
							+ " " + hour + ":" + minutes + " PM";
				}

				holder.txtUpdatedOn.setText(datefinal);
			} else {
				holder.txtUpdatedOn.setText("");
			}
			if (dto.getCount() > 0) {
				holder.txtcount.setText(dto.getCount() + " images");
			} else {
				holder.txtcount.setText("");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return rowView;
	}
}
